<?php

namespace App\Console\Commands;

use App\Http\Model\CoolNumberShoppingMall\CoolNumber;
use App\Http\Model\CoolNumberShoppingMall\UseLog;
use App\Http\Model\CoolNumberShoppingMall\UserUseLog;
use App\Http\Model\System;
use App\Http\Model\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

/**
 * 初始化靓号库
 *
 * Created on 2021/12/7 15:49
 * @Author <yuxiang.zhu@asiainnovations.net>
 */
class InitCoolNumber extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cool:number:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化靓号库';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $coolNumbers = User::query()->select(['user_id', 'uuid'])->where('liang', 1)->get()->toArray();
        if ($coolNumbers) {
            $modelCoolNumber = new CoolNumber();
            foreach ($coolNumbers as $coolNumber) {
                $userId = $coolNumber['user_id'];
                $uuid = $coolNumber['uuid'];
                if (CoolNumber::query()->whereRaw('uuid="' . $uuid . '"')->count()) {
                    continue;
                }

                UseLog::query()->create(['uuid' => $uuid, 'user_id' => $userId]);// UUID使用记录
                UserUseLog::query()->create([
                    'old_uuid' => $uuid,
                    'new_uuid' => $uuid,
                    'user_id' => $userId,
                    'change_mode' => UserUseLog::CHANGE_BY_ADMINISTRATOR,
                ]);
                $type = $modelCoolNumber->getCoolNumberType($uuid);
                CoolNumber::query()->create(['uuid' => $uuid, 'type' => $type, 'is_use' => CoolNumber::IS_USE_YES, 'usage_times' => 1,]);
                echo 'user_id=' . $userId . ',uuid=' . $uuid . PHP_EOL;
            }
        }

        echo 'success' . PHP_EOL;
        exit();
    }
}
